/******************************************************************************
*  author: hnuweiwei@gmail.com
*  version: 1.0
*  file: myStack3.h
*  date: 2014.05.25
*  description: 栈的头文件声明与定义
*  other: 定义缺省模板实参，模板参数可以是类型，也可以是普通值
******************************************************************************/
#ifndef MYSTACK3_H_INCLUDED
#define MYSTACK3_H_INCLUDED


#include <vector>
#include <stdexcept>

template <typename T, typename CONT = std::vector<T> >
class MyStack {
  private:
    CONT elems;               // elements

  public:
    void push(T const&);      // push element
    void pop();               // pop element
    T top() const;            // return top element
    bool empty() const {      // return whether the stack is empty
        return elems.empty();
    }
};

template <typename T, typename CONT>
void MyStack<T,CONT>::push (T const& elem)
{
    elems.push_back(elem);    // append copy of passed elem
}

template <typename T, typename CONT>
void MyStack<T,CONT>::pop ()
{
    if (elems.empty()) {
        throw std::out_of_range("Stack<>::pop(): empty stack");
    }
    elems.pop_back();         // remove last element
}

template <typename T, typename CONT>
T MyStack<T,CONT>::top () const
{
    if (elems.empty()) {
        throw std::out_of_range("Stack<>::top(): empty stack");
    }
    return elems.back();      // return copy of last element
}



#endif // MYSTACK3_H_INCLUDED


#include<iostream>
#include "myStack3.h"
#include <deque>
#include <cstdlib>
using namespace std;


//int main()
//{
//    try {
//        // stack of ints:
//        MyStack<int> intStack;
//
//        // stack of doubles which uses a std::deque<> to manage the elements
//        MyStack<double,std::deque<double> > dblStack;
//
//        // manipulate int stack
//        intStack.push(7);
//        std::cout << intStack.top() << std::endl;
//        intStack.pop();
//
//        // manipulate double stack
//        dblStack.push(42.42);
//        std::cout << dblStack.top() << std::endl;
//        dblStack.pop();
//        dblStack.pop();
//        system("pause");
//    }
//    catch (std::exception const& ex) {
//        std::cerr << "Exception: " << ex.what() << std::endl;
//        system("pause");
//        return EXIT_FAILURE;  // exit program with ERROR status
//    }
//    system("pause");
//}
